<?php
class C_PaymentStatistics extends Controller {
    private $logger;
    const MAX_INSURANCE_ALLOWANCE_DAYS = 3;
    const MAX_REAL_PAYMENT_ALLOWANCE_DAYS = 7;

    function __construct($template_mod = "general") {
        parent::Controller();
        $this->template_mod = $template_mod;
        $this->logger = LoggerManager::getLogger('module.billing.controllers.C_PaymentStatistics');
        $this->view->path   =   "payment_statistics";
        if (Celini::getCurrentController() == 'PaymentStatistics') {
            $menu =& Menu::getInstance();
            $menu->currentSection = 'patient';
        }
    }


    function actionViewStatistics() {
        return $this->view->render('statistics.html');
    }

    public function actionViewRealPaymentStatistics() {
    	$result = array(
                    'error'     =>  array('code' => CoreErrorCode::NORMAL,
                                          'mess' => ''),
                    'result'    =>  ''
        );
        $config = Celini::configInstance();
        $max_days = $config->get('max_real_payment_allowance_days');
        $dayList = $this->fillDateRange($max_days, self::MAX_REAL_PAYMENT_ALLOWANCE_DAYS);
        $this->assign('dayList', $dayList);

        $cashierList = Celini::newORDO('Employee')->getCashierList();
        $this->assign('cashierList', $cashierList);
        $currentCashierId = $this->_me->get_person_id();
        $this->assign('currentCashierId', $currentCashierId);

        $deptList = Celini::newORDO('Department')->getDepartmentList();
        $this->assign('deptList', $deptList);
        $this->assign('selectedDept', $this->_me->get_department_id());

        $result['result']   =   $this->view->render("real_payment_statistics_content.html");
        return json_encode($result);
    }

    private function fillDateRange($configNumber, $maxAllowance) {
        if (is_numeric($configNumber) &&
            ($configNumber > $maxAllowance) || $configNumber < 0) {
            $configNumber = $maxAllowance;
        }
        $d = date('d');
        $y = date('Y');
        $m = date('m');
        $configNumber--;
        $dayList = array("$y-$m-$d" => L10nHelper::_("Today"));
        if ($configNumber-- > 0) {
            $d -= 1;
            $val = date ("Y-m-d", mktime(0,0,0,$m,$d,$y));
            $dayList[$val] = L10nHelper::_("Yesterday (1 day before)");
        }

        $start = 2;
        while ($configNumber-- > 0) {
            $d -= 1;
            $val = date ("Y-m-d", mktime(0,0,0,$m,$d,$y));
            $currentDay = date ("D", mktime(0,0,0,$m,$d,$y));
            $dayList[$val] = L10nHelper::_("%s (%s days before)",Utils::toVietnameseDay($currentDay) ,$start);
            $start++;
        }
        return $dayList;
    }

    public function actionRenderRealPaymentStatisticsAjax() {
        $startDate  = $_GET['start_date'];
        $endDate    = $_GET['end_date'];
        $cashierId  = $_GET['cashier_id'];
        $departmentId = $_GET['department_id'];
        if ($cashierId == 0) {
            $cashierId = false;
        }
        $result = array(
                    'error'     =>  array('code' => CoreErrorCode::NORMAL,
                                          'mess' => ''),
                    'result'    =>  ''
        );
        $paymentInfo    =   Celini::newORDO('PayReceipt')->getStatisticalPaymentInfo($departmentId, $startDate, $endDate, $cashierId);
        // Format number
        $totalPayment   =   0;
        foreach ($paymentInfo as &$payment) {
        	$totalPayment  +=  $payment['real_payment'];
            $payment['real_payment']    =   L10nHelper::renderNumber($payment['real_payment']);
        }

        $this->assign('paymentInfo', $paymentInfo);
        $this->assign('totalPayment', L10nHelper::renderNumber($totalPayment));
        $this->assign('index', 1);
        $result['result']   =   $this->view->render('real_payment_statistics_content.html');
        return json_encode($result);
    }
    
    public function actionViewInsuranceStatistics() {
        $result = array(
                    'error'     =>  array('code' => CoreErrorCode::NORMAL,
                                          'mess' => ''),
                    'result'    =>  ''
        );
        $config = Celini::configInstance();
        $max_days = $config->get('max_real_payment_allowance_days');
        $dayList = $this->fillDateRange($max_days, self::MAX_REAL_PAYMENT_ALLOWANCE_DAYS);
        $this->assign('dayList', $dayList);

        $cashierList = Celini::newORDO('Employee')->getCashierList();
        $this->assign('cashierList', $cashierList);
        $currentCashierId = $this->_me->get_person_id();
        $this->assign('currentCashierId', $currentCashierId);

        $deptList = Celini::newORDO('Department')->getDepartmentList();
        $this->assign('deptList', $deptList);
        $this->assign('selectedDept', $this->_me->get_department_id());

        $result['result']   =   $this->view->render("real_payment_statistics_content.html");
        return json_encode($result);
    }
}
?>